package bmCheckClassId.database;

import java.sql.*;

/**
 * Created by rslai on 2018/12/12.
 */
public class DatabaseMysql implements Database {
    Connection con = null; // 数据库连接

    /**
     * 构造函数
     * @param serverIP mysql服务器ip或域名
     * @param port mysql端口号
     * @param dataBaseName 数据库名称
     * @param userName 用户名
     * @param password 密码
     */
    public DatabaseMysql(String serverIP, int port, String dataBaseName, String userName, String password) {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://" + serverIP + ":" + port + "/" + dataBaseName;
        try {
            if (null == con || con.isClosed()) {
                Class.forName(driver); //加载驱动程序
                con = DriverManager.getConnection(url, userName, password);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public ResultSet query(String sql) throws SQLException {
        Statement statement = con.createStatement();
        return statement.executeQuery(sql);
    }
}
